package com.jxm.a12_1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2024-1-11 16:30
 * @Version: 1.0
 */
public class $Proxy0 extends Proxy implements Foo{

//    private InvocationHandler handler;

    public $Proxy0(InvocationHandler h){
        super(h);
    }

    static Method food;
    static Method bar;

    static {
        try {
            food = Foo.class.getDeclaredMethod("food");
            bar = Foo.class.getDeclaredMethod("bar");
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        }

    }

    @Override
    public void food() {
        try {
            //获取方法的对象
            h.invoke(this,food,new Object[0]);
        } catch (RuntimeException | Error e) { //运行异常
            throw e;
        }catch (Throwable e){ //检查异常
            throw new UndeclaredThrowableException(e);
        }

    }

    @Override
    public int bar() {
        try {
            return (Integer) h.invoke(this,bar,new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        }catch (Throwable e){
            throw new UndeclaredThrowableException(e);
        }
    }


}
